import type { ICartItem, TUpdateCartItemParams } from '@/types/cart'
import { http } from '@/utils/http'
import _ from 'lodash'

class CartAPI {
  /**
   * @param data {skuId:id,count:数量}
   * @returns 加入购物车
   */
  public static addCart(data: { skuId: string; count: number }) {
    return http({
      url: '/member/cart',
      method: 'POST',
      data,
    })
  }
  /**
   * @returns 获取购物车列表
   */
  public static getCartList() {
    return http<ICartItem[]>({
      url: '/member/cart',
      method: 'GET',
    })
  }
  /**
   * @returns 更新购物车单品
   */
  public static updateCartItem(data: TUpdateCartItemParams) {
    return http({
      url: `/member/cart/${data.skuId}`,
      method: 'PUT',
      data: _.omit(data, 'skuId'),
    })
  }

  /**
   * @returns 购物车全选/取消全选
   */
  public static updateAllSelected(selected: boolean) {
    return http({
      url: '/member/cart/selected',
      method: 'PUT',
      data: {
        selected,
      },
    })
  }

  /**
   * @returns 删除购物车
   * @params skuIds SKUID集合
   */
  public static deleteCartItem(skuIds: string[]) {
    return http({
      url: '/member/cart',
      method: 'DELETE',
      data: {
        ids: skuIds,
      },
    })
  }
}

export default CartAPI
